from django.http import HttpResponse
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
from studenthoods.apps.common.email_utils import send_email
from studenthoods.apps.contact.forms import ContactForm
from studenthoods.apps.contact.models import Contact
from settings import DEFAULT_FROM_EMAIL, EMAIL_CONTACT_RECEIVER

def contact(request):
    
    if request.method == 'POST':    
        form = ContactForm(request.POST)
        
        if form.is_valid():
            contact = Contact()
            contact.first_name = form.cleaned_data['first_name']
            contact.surname = form.cleaned_data['surname']
            contact.email = form.cleaned_data['email']
            contact.query = form.cleaned_data['query']
            contact.save()
            
            msg = contact.email + ' has contacted StudentHoods regarding... ' + contact.query
            
            send_email('StudentHoods: Contact us', msg, DEFAULT_FROM_EMAIL, EMAIL_CONTACT_RECEIVER)
            
            return redirect('contact_us_submitted')
    else:
        form = ContactForm()
    
    return render_to_response('contact/templates/contact.html', 
                              {'form':form}, 
                              context_instance=RequestContext(request))

def contact_submitted(request):
    return render_to_response('contact/templates/contact_submitted.html', {},
                              context_instance=RequestContext(request))
    